package com.financial.service.heemoney.service.impl;

import com.financial.service.heemoney.constants.FinancialSplitAccountHeemoneylogEnumConstants;
import com.financial.service.heemoney.convertor.HeemoneySplitAccountConvert;
import com.financial.service.heemoney.dao.FinancialSplitAccountHeemoneyDao;
import com.financial.service.heemoney.dao.FinancialSplitAccountHeemoneylogDao;
import com.financial.service.heemoney.domain.FinancialReportHeemoneyDO;
import com.financial.service.heemoney.domain.FinancialSplitAccountHeemoneyDO;
import com.financial.service.heemoney.domain.FinancialSplitAccountHeemoneylogDO;
import com.financial.service.heemoney.dto.HeemoneyConfigDTO;
import com.financial.service.heemoney.dto.businsess.HeemoneySplitAccountBusinessDTO;
import com.financial.service.heemoney.dto.commmon.HeemoneySplitAccoutCommonDTO;
import com.financial.service.heemoney.dto.inparameter.HeemoneySplitAccountInParamDTO;
import com.financial.service.heemoney.dto.param.HeemoneySplitAccountParamDTO;
import com.financial.service.heemoney.service.FinancialSplitAccountHeemoneyService;
import com.financial.service.heemoney.utils.JsonUtils;
import com.financial.service.heemoney.utils.OkHttpUtils;
import com.financial.service.heemoney.utils.ResultUtils;
import com.financial.service.heemoney.utils.apiresult.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class FinancialSplitAccountHeemoneyServiceImpl implements FinancialSplitAccountHeemoneyService {
    @Autowired
    private FinancialSplitAccountHeemoneyDao financialSplitAccountHeemoneyDao;
    @Autowired
    private FinancialSplitAccountHeemoneylogDao financialSplitAccountHeemoneylogDao;
    @Autowired
    private OkHttpUtils okHttpUtils;

    public static final Logger log = LoggerFactory.getLogger(FinancialWxconfigHeemoneylogServiceImpl.class);

    @Override
    public FinancialSplitAccountHeemoneyDO get(Integer id) {
        return financialSplitAccountHeemoneyDao.get(id);
    }

    @Override
    public Map<String, Object> splitAccount(HeemoneySplitAccountInParamDTO heemoneySplitAccountInParamDTO) {
        log.info("调用金融中心汇收银服务层付款分账注册接口前参数打印：{}", JsonUtils.beanToJson(heemoneySplitAccountInParamDTO));

        String resultStr = "";
        //分账账户业务处理---拼接调用参数（分为公共参数及其业务参数）
        HeemoneySplitAccoutCommonDTO heemoneySplitAccoutCommonDTO = HeemoneySplitAccountConvert.MAPPER.configDTOConvertCommonDTO(new HeemoneyConfigDTO(), heemoneySplitAccountInParamDTO);
        HeemoneySplitAccountBusinessDTO heemoneySplitAccountBusinessDTO = HeemoneySplitAccountConvert.MAPPER.configDTOConvertBusinessDTO(heemoneySplitAccountInParamDTO);
        HeemoneySplitAccountParamDTO heemoneySplitAccountParamDTO = new HeemoneySplitAccountParamDTO(heemoneySplitAccoutCommonDTO, heemoneySplitAccountBusinessDTO);
        log.info("http请求汇收银服务层付款分账账户注册接口入参：{}", heemoneySplitAccountParamDTO.getJsonParam());
        try {
            resultStr = okHttpUtils.postJsonParams(new HeemoneyConfigDTO().getHeemoneyPayQuery().getRequest_url(), heemoneySplitAccountParamDTO.getJsonParam());
            log.info("http请求汇收银服务层付款分账账户注册接口回执:{}", resultStr);
            //解析数据并保存付款分账账户业务表
            this.updateSplitAccout(heemoneySplitAccountInParamDTO, resultStr);
            //返回调用信息
            return ResultUtils.resultStr(resultStr);
        } catch (Exception e1) {
            e1.printStackTrace();
            log.error("http请求汇收银服务层付款分账账户注册平台商户号为：{}", heemoneySplitAccountInParamDTO.getStore_no());
        }
        return ResultUtils.resultStr(resultStr);
    }

    private void updateSplitAccout(HeemoneySplitAccountInParamDTO heemoneySplitAccountInParamDTO, String resultStr) {
        Map<String, Object> convertMap = JsonUtils.jsonToMap(resultStr);
        String result_code = convertMap.get("result_code").toString();
        String result_msg = convertMap.get("result_msg").toString();
        String mch_uid = heemoneySplitAccountInParamDTO.getMch_uid();
        Map<String, Object> map = new HashMap<>();
        map.put("mch_uid", mch_uid);
        map.put("is_delete", FinancialSplitAccountHeemoneylogEnumConstants.ENUM_ISDELETE_UNDEL);
        List<FinancialSplitAccountHeemoneylogDO> list = financialSplitAccountHeemoneylogDao.list(map);
        FinancialSplitAccountHeemoneylogDO splitAccountHeemoneylogDO = list != null && list.size() > 0 ? list.get(0) : null;
        splitAccountHeemoneylogDO.setGmtModified(new Date());
        if (result_code.equals("SUCCESS")) {
            splitAccountHeemoneylogDO.setState(FinancialSplitAccountHeemoneylogEnumConstants.ENUM_STATE_SUC);
            splitAccountHeemoneylogDO.setReturnMessage(result_msg);
        } else {
            splitAccountHeemoneylogDO.setReturnMessage(convertMap.get("error_msg").toString());
            splitAccountHeemoneylogDO.setState(FinancialSplitAccountHeemoneylogEnumConstants.ENUM_STATE_FAIL);
        }
        financialSplitAccountHeemoneylogDao.update(splitAccountHeemoneylogDO);
    }


    @Override
    public List<FinancialSplitAccountHeemoneyDO> list(Map<String, Object> map) {
        return financialSplitAccountHeemoneyDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return financialSplitAccountHeemoneyDao.count(map);
    }

    @Override
    public int save(FinancialSplitAccountHeemoneyDO financialSplitAccountHeemoney) {
        return financialSplitAccountHeemoneyDao.save(financialSplitAccountHeemoney);
    }

    @Override
    public int update(FinancialSplitAccountHeemoneyDO financialSplitAccountHeemoney) {
        return financialSplitAccountHeemoneyDao.update(financialSplitAccountHeemoney);
    }

    @Override
    public int remove(Integer id) {
        return financialSplitAccountHeemoneyDao.remove(id);
    }

    @Override
    public int batchRemove(Integer[] ids) {
        return financialSplitAccountHeemoneyDao.batchRemove(ids);
    }


}
